import urllib.request
from pyquery import PyQuery as pq


class HtmlDownloader(object):
    def download(self, url):
        if url is None:
            return None
        response = urllib.request.urlopen(url)
        if response.getcode() != 200:
            return None

        htmlcontent = response.read().decode('utf-8')
        doc = pq(htmlcontent)
        doc('.nav').html('<div class="nav"><div class="container"><div class="img"><a href="#" target="_blank"><img src="41260.files/logo.png" alt="logo"></a></div></div></div>')
        doc('.nav-bar').html('<div class="nav-bar hidden-print"><div class="container"><a class="logo navbar-header" href="#"><img class="logo" src="../../15746.files/logo.jpg" alt="中国军网"></a></div></div>')
        doc('.footer-content p').remove()
        doc('.sidebar').remove()
        doc('.content').css('padding-left','0')
        doc('#ArticleRelation').remove()
        doc('#loadNextPageBtn').remove()
        doc('#pageFooter').remove()
        doc('#share-to').remove()
        doc('.footer-content').remove()
        htmlcontent = doc.html()
        return htmlcontent
